﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using Abp.Runtime.Caching;
using FwProject.Distribution.Dto;
using Microsoft.AspNetCore.Hosting;
using X.PagedList;
using System.Linq;
using Abp.UI;
using FwProject.Users;

namespace FwProject.Distribution
{
    [AutoMap(typeof(DistributionLine))]
    public class DistributionLineAppService: FwProjectAppServiceBase, IDistributionLineAppService
    {
        public readonly IRepository<DistributionLine, Guid> _repositoryDistributionLine;
        public readonly IRepository<DriverLine, Guid> _repositoryDriverLine;
        public readonly IDriverLineAppService _driverLineAppService;
        public readonly IUserAppService _userAppService;
        public readonly ICacheManager _catchmanager;
        public readonly IHostingEnvironment _env;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }

        public DistributionLineAppService(IRepository<DistributionLine, Guid> repositoryDistributionLine, ICacheManager catchmanager, IHostingEnvironment env,
            IDriverLineAppService driverLineAppService, IRepository<DriverLine, Guid> repositoryDriverLine, IUserAppService userAppService)
        {
            _repositoryDistributionLine = repositoryDistributionLine;
            _driverLineAppService = driverLineAppService;
            _repositoryDriverLine = repositoryDriverLine;
            _catchmanager = catchmanager;
            _env = env;
            _userAppService = userAppService;
            _guid = SequentialGuidGenerator.Instance;
        }
        /// <summary>
        /// 添加配送线路
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(DistributionLineInput input)
        {
            var drivermodel = _repositoryDistributionLine.GetAll().Where(o=>o.LineName == input.LineName).FirstOrDefault();
            if (drivermodel != null)
                throw new UserFriendlyException("该线路已存在！");

            var model = _map.Map<DistributionLine>(input);
            model.Id = _guid.Create();
            model.FactoryInfoid = (await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value)).FactoryinfoId;
            model.CreationTime = DateTime.Now;
            return await _repositoryDistributionLine.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改配送线路
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Update(Guid id, DistributionLineInput input)
        {
            var model = await _repositoryDistributionLine.GetAsync(id);
            model = _map.Map(input, model);
            model.FactoryInfoid = (await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value)).FactoryinfoId;
            await _repositoryDistributionLine.UpdateAsync(model);
        }

        public async Task Delete(Guid id)
        {
            var model = await _repositoryDistributionLine.GetAsync(id);
            await _repositoryDistributionLine.DeleteAsync(model);
        }

        public async Task<DistributionLineDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryDistributionLine.GetAsync(id);
            return _map.Map<DistributionLineDto>(model);
        }
        /// <summary>
        /// 获取所有配送线路线路
        /// </summary>
        /// <returns></returns>
        public async Task<List<DistributionLineDto>> GetAllLineAsync()
        {
            var usermodel = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);
            var model =  _repositoryDistributionLine.GetAll().Where(o => o.FactoryInfoid == usermodel.FactoryinfoId).AsQueryable();
            return await model.ProjectTo<DistributionLineDto>().ToListAsync();
        }
        /// <summary>
        /// 查询线路
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<IPagedList<DistributionLineDto>> GetPagedListAsync(int pageIdx, int pageSize,string name)
        {
            var usermodel = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);
            var listmodel = _repositoryDistributionLine.GetAll().Where(o=>o.FactoryInfoid==usermodel.FactoryinfoId);

            if (!string.IsNullOrEmpty(name))
                listmodel = listmodel.Where(o=>o.LineName.Contains(name));

            return await listmodel.ProjectTo<DistributionLineDto>().ToPagedListAsync(pageIdx, pageSize);
        }
    }
}
